package demo;

public class Number {
    private int num = 1;//记录线程要打印的数字，初始值为1
    private boolean odd = true;//true表示奇数线程打印

    public synchronized void printOddNum(){
        if(odd){
            System.out.println(Thread.currentThread().getName()+":"+num++);
        }
        odd = false;
        this.notify();
        try {
            this.wait();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    public synchronized void printEvenNum(){
        if(!odd){
            System.out.println(Thread.currentThread().getName()+":"+num++);
        }
        odd = true;
        this.notify();
        try {
            this.wait();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    public int getNum() {
        return num;
    }
}
